<template>
	<view class="view">
		<view style='position: sticky;border-bottom: 1rpx solid #F1F1F1;'>
			<v-tabs :scroll="false" @change="changeTab" v-model="current" :tabs='tabs' activeColor="#000000" :lineScale="0.2"
				padding="0rpx" height="80rpx" line-height="6rpx" lineColor='#32C59A' currentColor='#00000'> </v-tabs>
		</view>
		<swiper easing-function='linear' :current="current" :style="{height: 'calc(100vh - 80rpx)'}" @change="swiperChange">
			<swiper-item v-for="(item, index) in tabs" :key='index'>
				<scroll-view scroll-y style="height: 100%;" @scrolltolower='scrolltolower'>
					<view v-if="index == 0" class="list">
						<view class="item" v-for="(citem,cindex) in list" :key="cindex" @click="readMessage(cindex)">
							<image :src="citem.operatorUserAvatar || '/static/images/default_avatar.png'" mode="aspectFill"></image>
							<view class="point" v-if="citem.isHaveRead == 0"></view>
							<view class="info">
								<view>
									<text>{{citem.operatorUserNickName}}</text>
									<text>{{citem.createTime}}</text>
								</view>
								<view>{{citem.operatorUserNickName}}{{citem.type | formatType}}</view>
							</view>
						</view>
					</view>
					<view v-else class="systemlist">
						<view class="item" v-for="(citem, cindex) in list" :key="cindex">
							<view class="time">{{ citem.createTime }}</view>
							<view class="info">
								<view class="title">{{ citem.infoTitle }}</view>
								<view class="text">
									<view v-html="citem.infoDetail" class="details" :class="citem.showMore ? 'showdetails' : ''"></view>
									<view class="foot" @click="operateDetail(cindex)">
										<text>{{ citem.showMore ? "收起" : "查看详情" }}</text>
										<image :class="citem.showMore ? 'img2' : ''" src="/static/images/ic_drop_down.png"></image>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {
		queryPageList
	} from "../../../apis/request.js";
	export default {
		data() {
			return {
				current: 0,
				tabs: ["互助消息", "系统消息"],
				list: [],
				finished: false,
				pageNo: 1,
			};
		},
		filters: {
			formatType(type) {
				switch (type) {
					case 0:
						return '点赞了你的帖子';
					case 1:
						return '取消点赞了你的帖子';
					case 2:
						return '关注了你';
					case 3:
						return '取消关注你';
					case 4:
						return '评论了你的帖子';
					case 5:
						return '点赞了你的评论';
					case 6:
						return '打赏了你的帖子';
					default:
						return '';
				}
			}
		},
		methods: {
			getList() {
				if (this.current == 1) {
					this.getNotificationList();
				} else {
					this.getMessageList();
				}
			},
			//滚动到底部
			scrolltolower() {
				this.getList();
			},
			changeTab(e) {
				this.current = e;
			},
			swiperChange(e) {
				this.current = e.detail.current;
				this.finished = false;
				this.pageNo = 1;
				this.list = [];
				this.getList();
			},
			async getNotificationList() {
				if (!this.finished) {
					const data = await queryPageList({
						pageNo: this.pageNo++,
						pageSize: 10,
					});
					if (data.records.length == 0) {
						this.finished = true;
					}
					this.list = this.list.concat(data.records.map(item => {
						this.$set(item, 'showMore', false);
						return item;
					}));
					uni.stopPullDownRefresh();
				}
			},
			getMessageList() {
				if (!this.finished) {
					this.$api.http('/user/message/info/list', 'GET', {
						pageNo: this.pageNo++,
						pageSize: 10,
					}, res => {
						if (res.data.result.records.length == 0) {
							this.finished = true;
						}
						this.list = this.list.concat(res.data.result.records);
						console.log(this.list);
						uni.stopPullDownRefresh();
					})
				}
			},
			operateDetail(index) {
				this.list[index].showMore = !this.list[index].showMore;
			},
			readMessage(index) {
				let message = this.list[index];
				if (message.isHaveRead == 1) {
					return this.goMessageDetail(message);
				}
				this.list[index].isHaveRead = 1;
				this.$api.http('/user/message/info/messageHasBeenRead', 'GET', {
					messageId: message.id,
					type: 1
				}, res => {
					this.goMessageDetail(message);
				});
			},
			goMessageDetail(message) {
				if (message.type == 2 || message.type == 3) {
					uni.navigateTo({
						url: '/pages/MinePages/friend/main?id=' + message.operatorObjectId
					})
				} else {
					uni.navigateTo({
						url: '/pages/CommunityPages/details/main?id=' + message.operatorObjectId
					})
				}
			},
		},
		onPullDownRefresh() {
			this.finished = false;
			this.pageNo = 1;
			this.list = [];
			this.getList();
		},
		onLoad() {
			Object.assign(this.$data, this.$options.data());
			this.getList();
		},
	};
</script>

<style lang="scss" scoped>
	.view {
		background-color: #f1efef;

		.systemlist {
			padding: 0 30rpx 30rpx;

			.item {
				margin-bottom: 0rpx;
				border-radius: 10rpx;
				padding: 30rpx;

				.time {
					margin-bottom: 25rpx;
					text-align: center;
					font-size: 22rpx;
					color: #999999;
				}

				.info {
					background: #ffffff;

					.title {
						padding: 30rpx;
						color: #020202;
						font-size: 30rpx;
						border-bottom: 1rpx solid #e8e8e8;
					}

					.text {
						padding: 30rpx;

						.details {
							color: #999999;
							font-size: 26rpx;
							line-height: 30rpx;
							height: 60rpx;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
							overflow: hidden;
						}

						.showdetails {
							height: auto;
							display: unset;
							overflow: unset;
							-webkit-box-orient: unset;
							-webkit-line-clamp: unset;
						}

						.foot {
							display: flex;
							align-items: center;
							margin-top: 30rpx;

							text {
								color: #020202;
								font-size: 26rpx;
								margin-right: 10rpx;
							}

							image {
								width: 15rpx;
								height: 8rpx;
							}

							.img2 {
								transform: rotate(180deg);
							}
						}
					}
				}
			}
		}

		.list {

			.item {
				position: relative;
				padding: 30rpx;
				background: #ffffff;
				display: flex;
				align-items: center;
				border-bottom: 1rpx solid #e8e8e8;

				image {
					width: 78rpx;
					height: 78rpx;
					border-radius: 50%;
					margin-right: 10rpx;
				}

				.point {
					display: inline-block;
					position: absolute;
					left: 80rpx;
					top: 25rpx;
					width: 20rpx;
					height: 20rpx;
					border-radius: 10rpx;
					background-color: red;
				}

				.info {
					flex: 1;

					view {
						font-size: 24rpx;
						color: #999999;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}

					view {
						display: flex;
						justify-content: space-between;
						align-items: center;

						text:nth-child(1) {
							flex: 1;
							color: #020202;
							font-size: 28rpx;
						}

						text:nth-child(2) {
							font-size: 24rpx;
							color: #999999;
						}
					}
				}
			}

			.item:nth-last-child(1) {
				border: none;
			}
		}
	}
</style>